const LOCAL_KEY = "todomvc"

/**
 * 从localstorage中获取目前所有任务
 */
export function fetch() {
    const result = localStorage.getItem(LOCAL_KEY);
    if (result) {
        return JSON.parse(result);
    }
    return [];
}

/**
 * 保存所有任务
 * @param {*} todos 任务列表
 */
export function save(todos) {
    localStorage.setItem(LOCAL_KEY, JSON.stringify(todos))
}

/**
 * 生成唯一编号
 */
export function generateId() {
    return Date.now() + Math.random.toString(16).substring(2, 4);
}

export function filter(todos, visibility = "all") {
    if (visibility === "all") {
        return todos;
    } else if (visibility === "active") {
        return todos.filter((it) =>
            !it.completed
        );
    } else if (visibility === "completed") {
        return todos.filter((it) =>
            it.completed
        );
    }
    throw new Error("invalid visibility value")
}